package com.code.leetcode.idea;

import java.util.Stack;

public class LeetCodeIdea020_02 {
    public static void main(String[] args) {
//        String s = "()[]{}";
//        String s = "()";
        String s = "(]";
        LeetCodeIdea020_02 leetCodeIdea020_02 = new LeetCodeIdea020_02();
        boolean valid = leetCodeIdea020_02.isValid(s);
        System.out.println(valid);

    }
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (Character c : s.toCharArray()) {
            if (stack.isEmpty()  &&  isRight(c)) {
                return false;
            }
            if (isLeft(c)) {
                stack.push(c);
            }
            if (isRight(c)) {
                Character popC = stack.pop();
                if (popC.equals('(') &&  !c.equals(')')) {
                    return false;
                } else if (popC.equals('[') &&  !c.equals(']')) {
                    return false;
                } else if (popC.equals('{') &&  !c.equals('}')) {
                    return false;
                }
            }

        }
        if (stack.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
    private  boolean isLeft(Character c) {
        return !isRight(c);
    }
    private  boolean isRight(Character c) {
        return c.equals('}') || c.equals(']') || c.equals(')');
    }

}
